package com.hugoo.imp.buzz;

import com.hugoo.core.Conf;
import com.hugoo.core.db.HSF;
import com.hugoo.core.db.mo.Record;
import com.hugoo.core.db.mo.RecordSet;
import com.hugoo.core.util.Logx;
import com.hugoo.core.util.TL;
import com.hugoo.imp.slt.ImpQueue;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Lovelock.Chan
 */
public class ImpDataBuzz extends Logx {
    
    private static ImpDataBuzz ref = new ImpDataBuzz();
    
    private ImpDataBuzz() {
    }
    
    public static ImpDataBuzz ref() {
        return ref;
    }
    
    public void saveAndValidationFileMeta(String sid, String scod, String itx, String impusr, String filename, String filepath, long filesize, String filext, String extpara) {
        //根据导入文件类型做基本数据验证
        String itye = Conf.getImpProp(sid, Conf.IMP._imp_type);
        String itb = Conf.getImpProp(sid, Conf.IMP._imp_table);
        String itc = Conf.getImpProp(sid, Conf.IMP._imp_table_column);
        if (TL.isNotEmpty(itx)) {//自定义导出模式
            itc = itx;
            log.info("采用自定义字段导入模式：" + itx);
        }
        String itm = Conf.getImpProp(sid, Conf.IMP._imp_methods);
        StringBuilder sql = new StringBuilder();
        //------------------------------------
        sql.append("insert into t_sys_imp_filemeta(sid,scod,cdate,impusr,filename,filepath,filesize,filext,verify_info,EXTPARA,"
                + "IMP_TABLENAME,IMP_COLUMN,IMP_METHOD) values ('").append(sid).append("','").append(scod).append("',sysdate,'").append(impusr).append("','").
                append(filename).append("','").append(filepath).append("','").append(filesize).append("','").append(filext).append("',empty_clob(),empty_clob()")
                .append(",'").append(itb).append("','").append(itc).append("','").append(itm).append("')");
        HSF.excute(null, sql.toString());
        //------------------------------------
        Map<String, String> info = new HashMap();
        if (TL.isEmpty(itb, itc, itm)) {
            info.put(FileValidation.VALI_RUST, "S");
            info.put(FileValidation.VALI_REASON, "无有效系统数据配置，请与管理员联系。");
        } else if (itm.matches("^[A|M]{1}[U|O]{1}[I|F|B]{1}[Z|D|N]{1}$")) {
            if ("TXT".equalsIgnoreCase(itye)) {
                new TxtFileValidation(sid, scod).validation(filepath, info);
                //完成文本类型验证
            } else if ("XLS".equalsIgnoreCase(itye)) {
                new XlsFileValidation(sid, scod).validation(filepath, info);
                //完成xls类型验证
            } else {
                info.put(FileValidation.VALI_REASON, "上传文件格式【" + itye + "】无效");
                info.put(FileValidation.VALI_RUST, "F");//验证失败
            }
        } else {
            info.put(FileValidation.VALI_RUST, "S");
            info.put(FileValidation.VALI_REASON, "系统无有效导入方法数据配置，请与管理员联系。");
        }
        sql = new StringBuilder();
        sql.append("update t_sys_imp_filemeta set ");
        String rust = info.get(FileValidation.VALI_RUST);
        String vr = info.get(FileValidation.VALI_REASON);
        boolean ip = false;
        if ("Y".equalsIgnoreCase(rust)) {//是否通过信息验证【基本YNWX】【审核YNWX-】【导入YNWX-】【是否成功YN-】
            //数据上传操作方式【A-AUTO(FTP自动获取),M-MANUAL(手工上传)】:
            //U-解压，O直接导入:
            //数据审核方式【I-OIMP(直接导入);F-FALI(先审后导),B-FILA(先导后审)】:
            //数据保留方式【Z-ZIP(zip压缩),D-DEL(删除),N-NONE(不做操作)】
            if (itm.contains("I")) {
                sql.append("ISPASS='YXW-',");
                ip = true;
            } else if (itm.contains("B")) {
                sql.append("ISPASS='Y-W-',");
                ip = true;
            } else {
                sql.append("ISPASS='YW--',");//F
            }
        } else {
            sql.append("ISPASS='NXNN',");
            rust = "N";
        }
        sql.append("VERIFY_REASON='").append(vr).append("',VRUST='").append(rust).append("' where scod='").append(scod).append("'");
        HSF.excute(null, sql.toString());
        //存储附加信息
        vr = info.get(FileValidation.VALI_INFO);
        HSF.saveClob(null, "select verify_info from t_sys_imp_filemeta where scod='" + scod + "' for update", "verify_info", vr);
        if (extpara != null) {//附加参数
            HSF.saveClob(null, "select EXTPARA from t_sys_imp_filemeta where scod='" + scod + "' for update", "EXTPARA", extpara);
        }
        //通过数据导入线程 
        if (ip) {
            ImpQueue.ref().putCode(scod);
        }
    }
    
    public static void main(String[] args) {
        System.out.println("AUID".matches("^[A|M]{1}[U|O]{1}[I|F|B]{1}[Z|D|N]{1}$"));
    }
    
    public String getMetaStatus(String scod) {
        return metaValue(scod, "ISPASS");
    }
    
    public boolean deleteImpData(String scod) {
        String tbName = metaValue(scod, "IMP_TABLENAME");
        StringBuilder delTable = new StringBuilder(), delMeta = new StringBuilder();
        if (TL.isNotEmpty(tbName) && !"-".equals(tbName)) {
            delTable.append("delete ").append(tbName).append(" where " + FileInfo.X__SCOD + "='").append(scod).append("'");
            HSF.excute(null, delTable.toString());
        }
        delMeta.append("delete t_sys_imp_filemeta where scod='").append(scod).append("'");
        int[] r = HSF.excute(null, delMeta.toString());
        for (int l : r) {
            if (l == -1) {
                return false;
            }
        }
        return true;
    }
    
    private String metaValue(String scod, String key) {
        StringBuilder sql = new StringBuilder();
        sql.append("select ").append(key).append(" from t_sys_imp_filemeta where scod='").append(scod).append("'");
        RecordSet<Record> rs = HSF.query(null, sql.toString());
        if (rs.next()) {
            return rs.getString(key);
        }
        return "-";
    }
}
